home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.ACO.net!alijku06!news
- From: Roland Exler <R.Exler@jk.uni-linz.ac.at>
- Newsgroups: comp.lang.c,comp.lang.c++,gnu.gcc.help,gnu.g++.help,comp.os.msdos.djgpp
- Subject: Re: float != float and floats as return types
- Date: 1 Feb 1996 07:40:13 GMT
- Organization: Institute for el. Measurement, University of Linz, Austria
- Message-ID: <4epqot$fj1@alijku06.edvz.uni-linz.ac.at>
- References: <4ej9lb$mpc@fu-berlin.de>
- NNTP-Posting-Host: sensor4.emt.uni-linz.ac.at
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
-
- axl@zedat.fu-berlin.de (Axel Thimm) wrote:
- >The next C++ example gives me surprising results:
- > #include <iostream.h>
- > #include <iomanip.h>
- > #include <math.h>
- > float quad( float );
- > int main() {
- > for( int i=0; i<10; ++i ) {
- > float a, b, c;
- > a = i/13.123123;
- > b = a*a;
- > c = quad(a);
- > cout << (b - c) << '\t';
- > cout << (b - a*a) << '\t';
- > cout << (c - quad(a)) << '\n';
- > }
- > return 0;
- > }
- > float quad( float x ) { return x*x; }
-
- >At first I thought all numbers should be zeros. The middle column might
- >not be zero, if the compiler calculates in higher precision than what
- >the variables are, so an intermediate storage (b) might loose some
- >digits, but I cannot understand what happens with c! Both times a
- >function is called, that _is_not_ inlined, so in both cases the same
- >loss of digits should be observed.
- >Any ideas?
-
- Hi Axel,
-
- Maybe the result of quad is returned on the coprocessor-stack, so it's uses
- with the full precicion even if the result is declared as float. (The
- coprocessor does _all_ computations using double or extended dependend on an
- internal flag.) In this case all floating-point results are truncated only if
- _stored_ to a var with less precicion. If you want to see more compile your
- program with gcc -c t_prec -save-temps and take a look on the file t_prec.s
- (the assembler-output for your program).
-
- Roland
-
- +---------------------------------------+---------------------------+
- I Roland Exler I EMAIL: I
- I Universitaet Linz I R.Exler@jk.uni-linz.ac.at I
- I Institut fuer Elektrische Messtechnik I I
- I Altenbergerstr. 69 I Phone: I
- I A-4040 Linz, AUSTRIA I + 43 732 2468 9205 I
- +---------------------------------------+---------------------------+
-
-
-